package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class NursingProjectServiceImpl implements NursingProjectService {

    private final NursingProjectMapper nursingProjectMapper;



    /**
     * 分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //计算分页查询
        PageHelper.startPage(pageNum, pageSize);

        List<NursingProjectVo> list = nursingProjectMapper.getByPage(name,status,pageNum,pageSize);

        Page<NursingProjectVo> page = (Page<NursingProjectVo>) list;

        return PageResponse.of(page,NursingProjectVo.class);
    }

    /**
     * 添加护理项目
     * @param nursingProjectDto
     */
    @Override
    public void save(NursingProjectDto nursingProjectDto) {

        NursingProject nursingProject = new NursingProject();

        BeanUtil.copyProperties(nursingProjectDto,nursingProject);

        Long userId = UserThreadLocal.getUserId();
        nursingProject.setCreateBy(userId);

        nursingProjectMapper.insert(nursingProject);
    }


    /**
     * 根据id查询项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {

        NursingProjectVo nursingProjectVo = nursingProjectMapper.selectById(id);

        return nursingProjectVo;
    }



    /**
     * 修改护理项目
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用或禁用
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        nursingProjectMapper.startOrStop(id,status);
    }

    /**
     * 根据id删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }
}
